﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace XUtility
{
    public class XBaseMasterPage : System.Web.UI.MasterPage
    {
        #region 成员字段
        private Boolean isWcf = false;
        private MethodInfo[] lstMethodInfo = null;
        private String wcfFun = String.Empty;
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            Type type = this.Page.GetType();
            this.lstMethodInfo = type.GetMethods();
            this.isWcf = ParamClass.ExistAjaxFun(out wcfFun);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (this.isWcf) // 使用 WCF 来处理业务
            {
                String str = String.Empty;
                String retn = null;
                String msg = null;

                MethodInfo method = lstMethodInfo.SingleOrDefault(q => q.Name.Equals(wcfFun));
                if (method.GetParameters().Length == 1)
                {
                    Object[] invokeArgs = new Object[] { "" };
                    retn = (String)method.Invoke(this.Page, invokeArgs);
                    msg = (String)invokeArgs[0];
                    str = !String.IsNullOrWhiteSpace(msg) ? msg : retn;
                }
                else
                {
                    str = (String)method.Invoke(this.Page, null);
                }

                Response.Write(str.Replace("\r\n", ""));
                Response.End();
            }
        }
    }
}
